#include <iostream>
#include <SDL.h>
#include <unistd.h>
#include <SDL_mixer.h>
#include <SDL_image.h>
using namespace  std;

bool init();
void kill();
bool loop();
void error(const char* info);

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
Mix_Music* music;
Mix_Chunk* sound;

int main(int argc, char** argv){

    if ( !init() )
    {
        pause();
        return 1;
    }

    while( loop())
    {
        SDL_Delay(10);
    }


    kill();

    return 0;
}


bool init()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0){
        error("Error initializing SDL: ");
        return false;
    }

    if( IMG_Init( IMG_INIT_PNG) < 0){
        error("Error IMG_init");
        pause();
        return false;
    }

    if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024) < 0)
    {
        error("Error MIX_OpenAudio");
        return false;
    }

    window = SDL_CreateWindow("Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if ( !window){
        error("Error SDL_CreateWindow");
        return false;
    }

    renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
    if ( !renderer) {
        error("Error SDL_CreateRenderer");
        return false;
    }

    SDL_Surface* buffer = IMG_Load("test.png");
    if ( !buffer ){
        error("Error IMG_Load: ");
        return false;
    }

    texture = SDL_CreateTextureFromSurface( renderer, buffer);
    SDL_FreeSurface(buffer);
    buffer = NULL;
    if ( !texture){
        error("Error SDL_CreateTextureFromSurface");
        return false;
    }    

    music = Mix_LoadMUS("music.wav");
    if( !music){
        error("Error load music");
        return false;
    }
    Mix_PlayMusic( music, -1);


    sound = Mix_LoadWAV("scratch.wav");
    if( !sound){
        error("Error load wav");
        return false;
    }

    return true;
}

void kill()
{
    SDL_DestroyTexture( texture);
    Mix_FreeMusic( music);
    Mix_FreeChunk( sound);
    texture = NULL;
    music = NULL;
    sound = NULL;

    SDL_DestroyRenderer( renderer);
    SDL_DestroyWindow( window);
    window = NULL;
    renderer = NULL;

    IMG_Quit();
    Mix_Quit();
    SDL_Quit();

}

bool loop()
{
    static const unsigned char* keys = SDL_GetKeyboardState( NULL );
    SDL_Event e;
    SDL_Rect dest;

    SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255 );
    SDL_RenderClear( renderer);

    while ( SDL_PollEvent(&e) != 0){
        switch (e.type)
        {
        case SDL_QUIT:
            return false;
        case SDL_KEYDOWN:
            if ( e.key.keysym.sym == SDLK_SPACE){
                if( Mix_PausedMusic() == 1){
                    Mix_ResumeMusic();
                }else {
                    Mix_PauseMusic();
                }
                

            } else if( e.key.keysym.sym == SDLK_ESCAPE){
                Mix_ResumeMusic();
            }
            break;
        case SDL_MOUSEBUTTONDOWN:
            Mix_PlayChannel(-1, sound, 0);
            break;

        default:
            break;
        }

    }

    SDL_RenderCopy( renderer, texture, NULL, NULL);
    SDL_RenderPresent( renderer);


    return true;
}


void error(const char* info)
{
    cout << "Error " << info << " SDL: " << SDL_GetError() << endl;
    pause();
}